/*******************************************************************************************************/
/****************************数据访问模块，负责支持数据远程访问和回调处理*******************************/
/*******************************************************************************************************/
import md5 from '../lib/md5.min'
import toolPackage from './toolPackage'
export default{
	jx_post: function(current_moudel, url, obj, successFunc, errorFunc, isClslzyload) {
		var type = obj.type || "POST";
		this.__common_ajax(current_moudel, type, url, obj, successFunc, errorFunc, isClslzyload);
	},
	jx_get: function(current_moudel, url, obj, successFunc, errorFunc, isClslzyload) {
		this.__common_ajax(current_moudel, "GET", url, obj, successFunc, errorFunc, isClslzyload);
	},
	__common_ajax : function(current_moudel, type, url, obj, successFunc, errorFunc, isClslzyload) {
		var _jx_async = obj["async"];
		if (toolPackage.isNull(_jx_async)) {
			_jx_async = true;
		}else{
			_jx_async = false;
		}
		var _jx_ajax_frame = $.ajax({
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			type: type,
			async: _jx_async,
			url: url,
			timeout: _wap_config.timeout,
			data: this.__common_encryptRequestParam(obj),
			success: function(res, status, header) {
				$('.waiting').hide();
				// try{
					if (!res) {

						toolPackage.dialogMsg(current_moudel,"无数据返回");
						
						return;
					}

					if (res["stat"]["code"] == "-300") {
						toolPackage.dialogMsg(current_moudel,"token失效,请重新登录");
						toolPackage.delCookie("_wtk");
						toolPackage.delCookie("_uid");
            			if(toolPackage.is_injxapp())
            				toolPackage.appurlOpen('LOGIN','');
						return;
					}else if (res["stat"]["code"] == "-360") {
						toolPackage.dialogMsg(current_moudel,"token错误");
						return;
					}else if (res["stat"]["code"] == "-160") {
						toolPackage.dialogMsg(current_moudel,"访问被拒绝");
						return;
					}else if (res["stat"]["code"] == "-260") {
						toolPackage.dialogMsg(current_moudel,"短信验证码错误");
						return;
					}

					if (res["stat"]["code"] != 0) {
						toolPackage.dialogMsg(current_moudel,"api错误(" + res["stat"]["code"] + ")");
						return;
					}

					if (res["content"].length == 0) {
						toolPackage.dialogMsg(current_moudel,"无数据");
						return;
					}

					var errorEntry = res["stat"]["stateList"][0];
					if(errorEntry["code"] == "-100"){
						toolPackage.dialogMsg(current_moudel,"服务器开小差!");
						return;
					}
					successFunc(res["content"][0], res["stat"]["stateList"][0]);

					if(isClslzyload){
			            $("img.lazy").lazyload({
			                 placeholder : "../../img/default.png",
			                 threshold : 2000
			            });
			        }
        			//toolPackage.initDefaultImg($('img'),_wap_config.linkUrl+"/img/default.png");

				// }catch(e){
				// 	toolPackage.dialog.msg("方法执行错误","error");
				// }
			},
			error: function() {
				$('.waiting').hide();
				if(!isClslzyload){
					if (errorFunc && (typeof errorFunc == "function")) {
						errorFunc();
						return;
					}
					toolPackage.dialogMsg(current_moudel,"网络未连接!");
				}
			},
			complete: function(XMLHttpRequest, status) {　　　　
				if (status == 'timeout') { //超时,status还有success,error等值的情况
					$('.waiting').hide();
					_jx_ajax_frame.abort();　
					toolPackage.dialogMsg(current_moudel,"网络异常，请稍后重试!");　
				}　　
			}
		});
	},
	 /**
	 * 签名算法
	 * @param params
	 */
	__common_encryptRequestParam:function (params) {
		try {
			params._sm = _wap_config.sm;
			params._domid = _wap_config.domid;
			params._ft = _wap_config.ft;
			params._aid = toolPackage.__jx_getAId();
			params._ch = toolPackage.__jx_getCh();
			params._uid = toolPackage.getUid();
			params._tk = toolPackage.getTk();
			params._app_wap_tk = toolPackage.getAppwaptk();
			var s = "";
			var keys = [];
			for (var k in params) {
				keys.push(k);
			}
			keys.sort();
			for (var i = 0; i < keys.length; i++) {
				s = s + keys[i] + '=' + params[keys[i]];
			}
			s += _wap_config.domainUrl;
			params._sig = md5(s);
			return params;
		} catch (e) {
			toolPackage.dialog.msg("本地加签错误","error");
		}
		return params;
	}
}